﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;

public partial class Comod_StorageManage : ATPages
{
    Function f = new Function();
    ZSql zs = new ZSql();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HFroid.Value = Request.Cookies["user"].Values["roleid"];//角色22:售后服务部 
            hfxs.Value = Request.QueryString["moduid"].ToString();
            lblPath.Text = f.getModuleidAdd(Request.QueryString["moduid"]);//当前位置
            yjTm1.Text = Request.QueryString["yjTm1"];
            yjTm2.Text = Request.QueryString["yjTm2"];
            SpecificationsName.Text = Request.QueryString["SpecificationsName"];
            BrandName.Text = Request.QueryString["BrandName"];
            ComoditiesName.Text = Request.QueryString["ComoditiesName"];
            ComodBarcode.Text = Request.QueryString["ComodBarcode"];
            TypeStocck.SelectedValue = Request.QueryString["TypeStocck"];
            AuditState.SelectedValue = Request.QueryString["AuditState"];
            ProcNum.SelectedValue = Request.QueryString["ProcNum"];
            initGvData();//邦定数据  
        }
    }
    private void initGvData()//初使化数据
    {
        string search = "";
        string sql = "SELECT * FROM (SELECT stockId,ComodBarcode, ProcNum,typeStock,typeStockId,ProcMoney, CONVERT(char(10), storageTime, 120) AS storageTime, EditUserName, AuditState,(select isnull(sum(number),0) from CS_Sales where ComodBarcode=a.comodBarcode and WarehouseId=a.WarehouseId and isDel=0) as saleNum, (SELECT WarehouseName FROM CS_Warehouse   WHERE WarehouseId = a.WarehouseId) AS WarehouseName, (SELECT Supplier FROM CS_Supplier WHERE SupplierId = a.SupplierId) AS SupplierName, (SELECT Brand  FROM Brand WHERE BrandId = a.BrandId) AS BrandName, (SELECT ComoditiesName FROM ComoditiesName  WHERE ComoditiesNameId = a.ComoditiesName) AS ComoditiesName, (SELECT Specifications FROM Specifications    WHERE SpecificationsId = a.ModelId) AS SpecificationsName, (SELECT Units FROM Units  WHERE UnitsId = a.UnitId) AS UnitsName FROM CS_Stock a ) Stock where 1=1 ";
        if (!string.IsNullOrEmpty(Request.QueryString["yjTm1"]) && !string.IsNullOrEmpty(Request.QueryString["yjTm2"]))
        {
            search += "&yjTm1=" + yjTm1.Text.Trim();
            search += "&yjTm2=" + yjTm2.Text.Trim();
            sql += " and storageTime>='" + Request.QueryString["yjTm1"] + "' and storageTime<='" + Request.QueryString["yjTm2"] + "' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["SpecificationsName"]))
        {
            search += "&SpecificationsName=" + Common.FormatParameter(SpecificationsName.Text.Trim());
            sql += " and SpecificationsName like'%" + Request.QueryString["SpecificationsName"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["BrandName"]))
        {
            search += "&BrandName=" + Common.FormatParameter(BrandName.Text.Trim());
            sql += " and BrandName like'%" + Request.QueryString["BrandName"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["ComoditiesName"]))
        {
            search += "&ComoditiesName=" + Common.FormatParameter(ComoditiesName.Text.Trim());
            sql += " and ComoditiesName like'%" + Request.QueryString["ComoditiesName"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["ComodBarcode"]))
        {
            search += "&ComodBarcode=" + Common.FormatParameter(ComodBarcode.Text.Trim());
            sql += " and ComodBarcode like'%" + Request.QueryString["ComodBarcode"] + "%' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["TypeStocck"]))
        {
            search += "&TypeStocck=" + Common.FormatParameter(TypeStocck.SelectedValue.Trim());
            sql += " and typeStockId =" + Request.QueryString["TypeStocck"];

        }
        if (!string.IsNullOrEmpty(Request.QueryString["AuditState"]))
        {
            search += "&AuditState=" + Common.FormatParameter(AuditState.SelectedValue.Trim());
            sql += " and AuditState ='" + Request.QueryString["AuditState"] + "' ";
        }
        if (!string.IsNullOrEmpty(Request.QueryString["ProcNum"]))
        {
            search += "&ProcNum=" + Request.QueryString["ProcNum"];
            sql += "  and ProcNum" + Request.QueryString["ProcNum"];
        }
        sql += " ORDER BY stockId DESC";

        //Response.Write(sql);
        //Response.End();
        PageNavigator1.Url = "../Comod/StorageManage.aspx?moduid=" + hfxs.Value + search;
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;

    }
    protected void linkbtnUpdate_Click(object sender, EventArgs e)//修改
    {
        string id = (sender as LinkButton).CommandArgument;
        Response.Redirect("StorageAdd.aspx?id=" + id + "&action=update&moduid=" + Request.QueryString["moduid"]);
    }

    protected void lnkbtnFlag_Click(object sender, EventArgs e)//审核
    {
        string id = (sender as LinkButton).CommandArgument;
        Response.Redirect("StorageAdd.aspx?id=" + id + "&action=flag&moduid=" + Request.QueryString["moduid"]);
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = "";
        if (!string.IsNullOrEmpty(yjTm1.Text) && !string.IsNullOrEmpty(yjTm2.Text.Trim()))
        {
            search += "&yjTm1=" + yjTm1.Text.Trim();
            search += "&yjTm2=" + yjTm2.Text.Trim();
        }
        if (!string.IsNullOrEmpty(SpecificationsName.Text.Trim()))
        {
            search += "&SpecificationsName=" + Common.FormatParameter(SpecificationsName.Text.Trim());
        }
        if (!string.IsNullOrEmpty(BrandName.Text.Trim()))
        {
            search += "&BrandName=" + BrandName.Text.Trim();
        }
        if (!string.IsNullOrEmpty(ComoditiesName.Text.Trim()))
        {
            search += "&ComoditiesName=" + ComoditiesName.Text.Trim();
        }
        if (!string.IsNullOrEmpty(ComodBarcode.Text))
        {
            search += "&ComodBarcode=" + Common.FormatParameter(ComodBarcode.Text.Trim());
        }
        if (!string.IsNullOrEmpty(TypeStocck.SelectedValue.Trim()))
        {
            search += "&TypeStocck=" + Common.FormatParameter(TypeStocck.SelectedValue.Trim());
        }
        if (!string.IsNullOrEmpty(AuditState.SelectedValue.Trim()))
        {
            search += "&AuditState=" + Common.FormatParameter(AuditState.SelectedValue.Trim());
        }
        if (!string.IsNullOrEmpty(ProcNum.Text))//库存
        {
            search += "&ProcNum=" + Common.FormatParameter(ProcNum.Text);
        }
        Response.Write(TypeStocck.SelectedValue);
        //Response.End();
        PageNavigator1.Url = "../Comod/StorageManage.aspx?moduid=" + hfxs.Value + search;
        Response.Redirect("StorageManage.aspx?moduid=" + hfxs.Value + search);
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("StorageAdd.aspx?moduid=" + Request["moduid"] + "&action=add");
    }
    protected void btnDel_Click(object sender, EventArgs e)//删除操作
    {
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].FindControl("ckFlag") as CheckBox;
                if (ck.Checked)
                {
                    string id = this.GvData.Rows[i].Cells[0].Text;
                    zs.Open("delete CS_Stock  where StockId=" + id);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);
                }
                else
                {
                    n++;
                }
                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }
            }
        }
        zs.Close();
    }

    protected void btnallflag_Click(object sender, EventArgs e)//审核操作
    {
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].FindControl("ckFlag") as CheckBox;
                if (ck.Checked)
                {
                    string id = this.GvData.Rows[i].Cells[0].Text;
                    zs.Open("Update  CS_Stock set AuditState='已通过',AuditUserName='" + Server.UrlDecode(Request.Cookies["user"].Values["name"]) + "',AuditDate=getdate() where StockId=" + id);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('批量审核成功');", true);
                }
                else
                {
                    n++;
                }
                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }
            }
        }
        zs.Close();
    }
    protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)//权限设置
    {
        f.checkLevels(Request.Cookies["user"].Values["roleid"], Request["moduid"], 11, GvData, btnDel);
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int saleNum = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "saleNum"));
            int ProcNum = Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "ProcNum"));
            if (saleNum > 0)
            {
                e.Row.Cells[16].Enabled = false;
            }
        }
    }
}
